package com.hb.software.worker.impl;

import com.hb.software.entity.worker.Worker;
import com.hb.software.service.impl.BaseServiceImpl;
import com.hb.software.worker.WorkerService;
import com.hb.software.worker.dao.WorkerDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class WorkerServiceImpl extends BaseServiceImpl<Worker, Long> implements WorkerService {

    private static final Logger logger = LogManager.getLogger(WorkerServiceImpl.class.getName());

    private WorkerDao workerDao;

    @Autowired
    public void setWorkerDao(WorkerDao workerDao) {
        this.workerDao = workerDao;
        this.dao = this.workerDao;
    }

    @Override
    public List findByName(String name) {
        List list = workerDao.findByName(name);
        return list;
    }

    @Override
    public List findByPhone(String phone) {
        List list = workerDao.findByPhone(phone);
        return list;
    }

    @Override
    public List<Worker> findByDepartment(Long did) {
        return workerDao.findByDepartment(did);
    }

    @Override
    public long findLastId() {
        try {
            return workerDao.findLastId();
        } catch (Exception e) {
            return 0;
        }


    }


//    @Override
//    public List findByDepartment(String department) {
//        return workerDao.findByDepartment(department);
//    }

    @Override
    public String createExcel(List<Worker> list, String path) {

        // 创建一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = workbook.createSheet("员工表一");
        // 添加表头行
        HSSFRow hssfRow = sheet.createRow(0);
        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
//    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 添加表头内容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("编号");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("姓名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("年龄");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("性别");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(4);
        headCell.setCellValue("部门");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(5);
        headCell.setCellValue("电话");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(6);
        headCell.setCellValue("邮箱");
        headCell.setCellStyle(cellStyle);

        // 添加数据内容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 1);
            Worker worker = list.get(i);

            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(worker.getId());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(1);
            cell.setCellValue(worker.getName());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(2);
            cell.setCellValue(worker.getAge());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(3);
            cell.setCellValue(worker.getSex());
            cell.setCellStyle(cellStyle);

//            cell = hssfRow.createCell(4);
//            cell.setCellValue(worker.getDepartment());
//            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(5);
            cell.setCellValue(worker.getPhone());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(6);
            cell.setCellValue(worker.getEmail());
            cell.setCellStyle(cellStyle);
        }

        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream(path);
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }


}

